package com.lvmama.controller.rhino.system;

import com.lvmama.client.rhino.RoleClient;
import com.lvmama.client.rhino.UserClient;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.pojo.base.Page;
import com.lvmama.pojo.dto.rhino.system.RoleQueryDTO;
import com.lvmama.pojo.dto.rhino.system.UserDTO;
import com.lvmama.pojo.dto.rhino.system.UserQueryDTO;
import com.lvmama.pojo.entity.rhino.system.Role;
import com.lvmama.pojo.entity.rhino.system.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName : UserController
 * @Description : 用户管理
 * @Author :Administrator
 * @Date : 2018/8/23 17:18
 * @Version V1.0
 **/
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserClient userClient;
    @Autowired
    private RoleClient roleClient;

    /**
     * 获取用户列表
     * @param model
     * @param userQueryDTO
     * @return
     */
    @RequestMapping(value = "userList",method = RequestMethod.GET)
    public String getUserList(Model model, UserQueryDTO userQueryDTO){
        userQueryDTO.setLimit(userQueryDTO.getLimit());
        userQueryDTO.setPage(true);
        Page<User> userPage = userClient.getUserPage(userQueryDTO).getData();
        model.addAttribute("page",userPage);
        return "system/userList";
    }

    /**
     * 新增用户页
     * @param model
     * @return
     */
    @RequestMapping(value = "newUser",method = RequestMethod.GET)
    public String newUser(Model model){
        RoleQueryDTO query = new RoleQueryDTO();
        List<Role> result = roleClient.getRoleList(query).getData();
        model.addAttribute("roles", result);
        model.addAttribute("user",new User());
        return "system/userForm";
    }

    /**
     * 保存用户
     * @param userDTO
     * @return
     */
    @RequestMapping(value = "saveUser",method = RequestMethod.POST)
    @ResponseBody
    public Result saveUser(UserDTO userDTO){
        log.info("UserController saveUser start");

        UserQueryDTO query =new UserQueryDTO();
        query.setUsername(userDTO.getLoginName());
        List<User> userList = userClient.getUserList(query).getData();
        if (userList!=null&&!userList.isEmpty()&&userDTO.getId()==null){
            log.info("UserController saveUser error:user_exist");
            return Result.error(CodeMessage.USER_EXIST);
        }
        try{
            if(userDTO.getId()==null){
                //保存新用户
                userClient.saveUserByUserDTO(userDTO);
            }else{
                //用户更新
                userClient.updateUserByUserDTO(userDTO);
            }
        }catch (Exception e){
            log.info("UserController saveUser start error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("UserController saveUser end");
        return  Result.success(true);
    }

    /**
     * 编辑用户页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "editUser",method = RequestMethod.GET)
    public String editUser(Model model,String id){
        RoleQueryDTO query = new RoleQueryDTO();
        List<Role> roleList = roleClient.getRoleList(query).getData();
        if(roleList!=null){
            model.addAttribute("roles",roleList);
        }
        Result<User> userResult = userClient.getUserRoleById(id);
        if(userResult.getCode() ==0&&userResult.getData()!=null){
            model.addAttribute("user",userResult.getData());
        }
        return "system/userForm";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteUser",method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> deleteUser(String id){
        log.info("uerController deleteUser start");
        try{
            userClient.deleteUserById(id).getData();
        }catch (Exception e){
            log.error("userController deleteUser error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("uerController deleteUser end");
        return Result.success(true);
    }

    /**
     * 启用、停用用户
     * @param id
     * @return
     */
    @RequestMapping(value = "enableUser",method = RequestMethod.POST)
    @ResponseBody
    public Result enableUser(String id){
        log.info("request enableUser start.");
        try{
            Result<Integer> result = userClient.enableUserById(id);
        }catch (Exception e){
            log.error("enableUser error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("request enableUser end.");
        return Result.success(true);
    }
}
